using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using mojoPortal.Web.UI;
using eStore.Business;
using eStore.Helpers;
using Resources;
using System.Collections;
using mojoPortal.Business;
using ud_Utilities;

namespace eStore.UI
{
    public partial class ProductDetailPage : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        private Guid productGuid = Guid.Empty;
        private Store store;
        private Product product;
        private decimal productPrice;
        protected bool productHasMoreThanOneProduct = true;
        protected CultureInfo currencyCulture = CultureInfo.CurrentCulture;
        protected string teaserFileBaseUrl = string.Empty;
        private eStoreConfiguration config = new eStoreConfiguration();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!UserCanViewPage(moduleId))
                {
                    SiteUtils.RedirectToAccessDeniedPage();
                    return;
                }

                LoadSettings();

                if ((store == null) || (store.IsClosed))
                {
                    WebUtils.SetupRedirect(this, SiteUtils.GetCurrentPageUrl());
                    return;
                }

                SetupCss();
                PopulateLabels();
                PopulateControls();
            }
        }

        private void PopulateControls()
        {
            if (product == null) { return; }

            if (!product.IsVisible)
            {
                lnkAddToCart.Visible = false;

                if (!UserCanEditModule(moduleId)) { return; }
            }

            TitleControl.Header = eStoreResources.ProductDetail;
            litProductDescription.Text = product.Description;
            lblPrice.Text = StoreHelper.FormatPrice(productPrice.ToString(), currencyCulture);
            lblName.Text = product.Name;
            imgProduct.ImageUrl = ImageSiteRoot + "/" + ConfigurationManager.AppSettings["eStoreImagesFolder"] + Utilities.GetRealImageUrl(product.ImageUrl);

            litInfo.Text = eStoreResources.ProductBasicInfo;
            litDetail.Text = eStoreResources.ProductDetailInfo;

            lblCategory.Text = product.CategoryName;
            lblPrice.Visible = config.ShowPriceSetting;
            pnlProductDetails.CssClass += " " + config.CustomCssClassSetting + "-detail";
            lnkAddToCart.NavigateUrl = SiteRoot + "/eStore/CartAdd.aspx?Product="
                + product.Guid
                + "&pageid=" + pageId.ToString(CultureInfo.InvariantCulture)
                + "&mid=" + moduleId.ToString(CultureInfo.InvariantCulture);
            MetaDescription = product.MetaDescription;
            MetaKeywordCsv = product.MetaKeywords;
            AdditionalMetaMarkup = product.CompiledMeta;

            eStoreDB repository = new eStoreDB();
            List<ud_eStoreProduct> listSimilar = repository.GeteStoreOtherSimilarProduct(store.Guid, productGuid, product.CategoryID);
            if (listSimilar.Count == 0)
            {
                pnlSimilarOthers.Visible = false;
            }
            else
            {
                gvSimilarOthers.DataSource = listSimilar;
                gvSimilarOthers.DataBind();
                if (listSimilar.Count <= 10)
                {
                    gvSimilarOthers.AllowPaging = false;
                }
            }
            List<ud_eStoreProduct> listOthers = repository.GeteStoreOtherProduct(store.Guid, productGuid);
            if (listOthers.Count == 0)
            {
                pnlProductOthers.Visible = false;
            }
            else
            {
                gvProductOthers.DataSource = listOthers;
                gvProductOthers.DataBind();
                if (listOthers.Count <= 10)
                {
                    gvProductOthers.AllowPaging = false;
                }
            }
        }

        public string FormatProductUrl(string url)
        {
            return SiteRoot + url;
        }
        
        private void PopulateLabels()
        {
            if (product == null) return;
            Title = SiteUtils.FormatPageTitle(siteSettings, product.Name);
            lnkAddToCart.Text = eStoreResources.AddToCartLink;
            lblSimilarOthers.Text = eStoreResources.ProductOthersSimilar;
            lblProductOthers.Text = eStoreResources.ProductOthers;
        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", moduleId);
            productGuid = WebUtils.ParseGuidFromQueryString("Product", productGuid);

        }

        private void LoadSettings()
        {
            SiteUtils.GetCommerceConfig();
            store = StoreHelper.GetStore();
            currencyCulture = ResourceHelper.GetCurrencyCulture(siteSettings.GetCurrency().Code);

            if ((store != null) && (productGuid != Guid.Empty))
            {
                product = new Product(productGuid);
                productPrice = product.Price;
            }

            teaserFileBaseUrl = WebUtils.GetSiteRoot() + "/Data/Sites/" + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture)
                + "/estoreproductpreviewfiles/";

            ScriptConfig.IncludeYahooMediaPlayer = true;

        }

        protected virtual void SetupCss()
        {
            // older skins have this
            if (Page.Master == null) return;
            StyleSheet stylesheet = (StyleSheet)Page.Master.FindControl("StyleSheet");
            if (stylesheet == null) return;
            if (stylesheet.FindControl("styleestore") != null) return;
            Literal cssLink = new Literal
            {
                ID = "styleestore",
                Text = @"<link href='"
                       + SiteUtils.GetSkinBaseUrl()
                       +
                       @"styleestore.css' type='text/css' rel='stylesheet' media='screen' />"
            };
            stylesheet.Controls.Add(cssLink);
        }

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            Load += Page_Load;
            SuppressPageMenu();
            SuppressGoogleAds();

        }
        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            config = new eStoreConfiguration(settings);
            LoadSideContent(config.ShowLeftPanelSetting, config.ShowRightPanelSetting, config.ShowTopPanelSetting, config.ShowBottomPanelSetting, config.ShowCenterPanelSetting);
        }
        #endregion
    }
}
